﻿using System;
using Nofs.Net.nofs.metadata.interfaces;
using biz.ritter.javapi.lang;

namespace Nofs.Net.nofs.metadata.XMLDriver
{
    public sealed class Factory : IMetadataFactory
    {
        private XMLFileSystem _fileSystem;
        private XMLClassLoader _classLoader;
        private XMLAttributeAccessor _accessor;

        public IAttributeAccessor CreateAttributeAccessor() //throws Exception 
        {
            if (_fileSystem == null)
            {
                throw new System.Exception("File system is not yet initialized!");
            }
            if (_classLoader == null)
            {
                throw new System.Exception("Class loader must be initialized first");
            }
            if (_accessor == null)
            {
                _accessor = new XMLAttributeAccessor(_fileSystem, _classLoader);
            }
            return _accessor;
        }

        public INoFSClassLoader CreateClassLoader(String fileName) //throws Exception 
        {
            if (_fileSystem == null)
            {
                _fileSystem = new XMLFileSystem();
                _fileSystem.ReadFromXML(fileName + ".xml");
            }
            if (_classLoader == null)
            {
                _classLoader = new XMLClassLoader(_fileSystem, fileName);
            }
            return _classLoader;
        }

        public INoFSClassLoader CreateClassLoader(ClassLoader loader) //throws Exception 
        {
            throw new System.Exception("not supported");
        }

        public IMethodFilter CreateMethodFilter() //throws Exception 
        {
            if (_fileSystem == null)
            {
                throw new System.Exception("File system is not yet initialized!");
            }
            if (_accessor == null)
            {
                throw new System.Exception("Attribute accessor has not been initialized!");
            }
            return new XMLMethodFilter(_fileSystem, _accessor);
        }
    }
}
